<template>
	<view>
		<uni-forms ref="form" :rules="{}">
			<uni-forms-item class="inpit" label="用户名" name="username">
				<uni-easyinput v-model="user.name" placeholder="请输入用户名" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="邮箱" name="username">
				<uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="电话" name="username">
				<uni-easyinput v-model="user.telephone" placeholder="请输入电话" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="学院" name="username">
				<uni-easyinput v-model="user.department" placeholder="请输入学院" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="专业" name="username">
				<uni-easyinput v-model="user.major" placeholder="请输入学院" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="密码" name="username">
				<uni-easyinput v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>
			<uni-forms-item class="inpit" label="确认密码" name="username">
				<uni-easyinput v-model="user.uspassword" placeholder="再次输入密码" />
			</uni-forms-item>
			<uni-forms-item label="" name="">
				<button type="primary" @click="register">注册</button>
			</uni-forms-item>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					name: '',
					email: '',
					telephone: '',
					department: '',
					major: '',
					password: '',
					uspassword: ''
				},
				school: ['信息工程学院', '智能制造学院', '经济管理学院', '文法学院', '网络技术学院'],
				city: '',
				porfs: ['移动应用开发', '物联网应用技术', '艺术设计', '软件技术', '金融管理', '电子商务', '市场营销'],
				po: ''
			}
		},
		methods: {
			register(e) {
				if (this.user.name == '') {
					uni.showToast({
						title: '用户名不能为空',
						icon: 'error'
					});
					return

				}
				if (this.user.email == '') {
					uni.showToast({
						title: '邮箱不能为空',
						icon: 'error'
					});
					return

				}
				if (this.user.telephone == '') {
					uni.showToast({
						title: '手机号不能为空',
						icon: 'error'
					});
					return
				}
				if (this.user.department == '') {
					uni.showToast({
						title: '学院不能为空',
						icon: 'error'
					});
					return
				}
				if (this.user.major == '') {
					uni.showToast({
						title: '专业不能为空',
						icon: 'error'
					});
					return
				}	
				if (this.user.password == '') {
					uni.showToast({
						title: '密码不能为空',
						icon: 'error'
					});
					return
				}	
				if (this.user.uspassword == '') {
					uni.showToast({
						title: '二次密码不能为空',
						icon: 'error'
					});
					return
				}
				if (this.user.password !== this.user.uspassword) {
					uni.showToast({
						title: '两次输入的密码不一致',
						icon: 'error'
					});
					return
				} else {
					uni.request({
						url: 'http://localhost:8090/freshmenapp/user/register',
						method: 'POST',
						data: this.user,
						success: res => {
							console.log(res);
							if (res.data.success == true) {
								uni.showToast({
									title: '注册成功',
									icon: 'success'
								});
								uni.navigateBack({
									url:"../login/login"
								})
							} else {
								uni.showToast({
									title: '注册失败',
									icon: 'error'
								});
							}
						},
						fail: () => {
							uni.showToast({
								title: '网络错误',
								icon: 'error'
							});
						},
						complete: () => {}
					});
				}
			}
		}
	}
</script>

<style>
	.inpit {
		top: 50rpx;
		margin-left: 20rpx;
		margin-right: 30rpx;
	}

	.combox {
		top: 50rpx;
		margin-left: 20rpx;
		margin-right: 30rpx;
	}

	button {
		top: 50rpx;
		margin-left: 50rpx;
		margin-right: 50rpx;
	}
</style>